const db = uniCloud.database()
const dbCmd = db.command;
module.exports = {
	main: async (event) => {
		let {
			res,
			data,
			action
		} = event;
		switch (action) {
			case 'query':
				res.data = await getData(data);
				break;
			case 'queryById':
				res.data = await getDataById(data);
				break;
			case 'add':
				res = await addData(data);
				break;
			case 'edit':
				res = await editData(data);
				break;
			case 'del':
				res = await delData(data);
				break;
			default:
				res = {
					code: 402,
					msg: '非法访问'
				}
				break;
		}
		return res;
	}
}

/**
 * 获取列表数据
 */
async function getData(data) {
	var result = await db.collection('shipping_template').where({
			'_id': dbCmd.neq('null'),
			'temName': eval('/' + data.name + '/')
		})
		.skip((data.page-1)*data.limit)
		.limit(data.limit)
		.orderBy('updatetime', 'desc')
		.get()
	var resultTotal = await db.collection('shipping_template').where({
			'_id': dbCmd.neq('null'),
			'temName': eval('/' + data.name + '/')
		})
		.count()
	let res = {
		total:resultTotal.total,
		records:result.data
	}
	return res
}
/**
 * 获取数据通过ID
 */
async function getDataById(data) {
	var result = await db.collection('shipping_template').doc(data.id)
		.get()
	return result.data
}

/**
 * 添加数据
 */
async function addData(data) {
	var res = {}
	var tmp = await db.collection('shipping_template').where({
		'temName': data.temName
	}).get()
	if (tmp.data[0]) {
		res.code = 402;
		res.msg = '模板名称已存在，请重新输入'
	} else {
		const result = await db.collection('shipping_template').add({
			feeSetting: data.feeSetting,
			deliveryData: data.deliveryData,
			temName: data.temName,
			publicFee: data.publicFee,
			billingMode: data.billingMode,
			updatetime: data.updatetime
		})
		res.code = 200;
		res.data = result.data
	}
	return res;
}
/**
 * 更新数据
 */
async function editData(data) {
	var res = {}
	var t1 = await db.collection('shipping_template').where({
		'_id': data.id
	}).get();
	if (t1.data[0].temName == data.temName) {
		const result = await db.collection('shipping_template').doc(data.id).update({
			feeSetting: data.feeSetting,
			deliveryData: data.deliveryData,
			temName: data.temName,
			publicFee: data.publicFee,
			billingMode: data.billingMode,
			updatetime: data.updatetime
		})
		res.code = 200
		res.data = result.data
	} else {
		var t2 = await db.collection('shipping_template').where({
			'temName': data.temName
		}).get()
		if (t2.data[0]) {
			res.code = 402;
			res.msg = '模板名称已存在，请重新输入'
		} else {
			const result = await db.collection('shipping_template').doc(data.id).update({
				feeSetting: data.feeSetting,
				deliveryData: data.deliveryData,
				temName: data.temName,
				publicFee: data.publicFee,
				billingMode: data.billingMode,
				updatetime: data.updatetime
			})
			res.code = 200
			res.data = result.data
		}
	}
	return res;
}
/**
 * @param {Object} data
 * 删除数据
 */
async function delData(data) {
	var res = {}
	var tmp = await db.collection('shipping_template').doc(data._id).remove()
	if (tmp.deleted === 1) {
		res.code = 200
		res.msg = '删除成功'
	} else {
		res.code = 402
		res.data = '删除失败'
	}
	return res
}
